package com.sdkx.app.datalayers;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;

import com.sdkx.app.R;
import com.sdkx.app.model.Mark;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import cn.creable.gridgis.display.Display;
import cn.creable.gridgis.geodatabase.Feature;
import cn.creable.gridgis.geodatabase.FeatureClass;
import cn.creable.gridgis.geodatabase.IFeature;
import cn.creable.gridgis.geometry.GeometryType;
import cn.creable.gridgis.geometry.Point;
import cn.creable.gridgis.util.Image;

public class MarkPointsPanel extends MapDraw {
    Paint paint;
    Image image;
    private FeatureClass fc;
    List<Mark> marks = new ArrayList<>();
    List<Bitmap> imgeList = new ArrayList<>();
    public MarkPointsPanel(Context context) {
        super(context);
        paint = new Paint();
        paint.setTextSize(18);
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        BitmapDrawable bmpDraw = (BitmapDrawable) appContext.getResources().getDrawable(R.drawable.airplane);
        image = new Image(bmpDraw.getBitmap());
        fc = new FeatureClass(new String[]{"text"}, GeometryType.Point);
    }

    @Override
    protected void draw2(Canvas canvas, Display display) {
        float zoom  = display.getDisplayTransformation().getZoom();
        if(zoom <2.764279e-02f) {
            Canvas oldG = display.getCanvas();
            display.setCanvas(canvas);
            if (imgeList.size() > 0) {
                for (Mark mark : marks) {
                    display.DrawImagePoint(new Point(mark.getDoubleLongitude(), mark.getDoubleLatitude()),
                            new Image(imgeList.get(mark.getType())));
                }
            }
            display.setCanvas(oldG);
            display.setLabelSize(25);
            display.setLabelColor(Color.RED);
            display.labelFeatureClass(canvas, fc, 0,32, 16);
        }
    }


    @Override
    public void upData() {
        marks =  LitePal.where("isshow=0").find(Mark.class);
        Vector<IFeature> fts = new Vector<IFeature>();
        for(Mark mark : marks){
            IFeature ft = new Feature(new Point(mark.getDoubleLongitude(),mark.getDoubleLatitude()), new String[]{mark.getName()});
            fts.addElement(ft);
        }
        // 设置全部地图要素
        fc.setFeatures(fts);
        if(imgeList.size() == 0){
          Bitmap bitmap  =   ((BitmapDrawable) appContext.getResources().getDrawable(R.drawable.mark_type1)).getBitmap();
            Bitmap newicon =  Bitmap.createScaledBitmap(bitmap,32,32,false);
            imgeList.add(newicon);
            bitmap.recycle();
             bitmap  =   ((BitmapDrawable) appContext.getResources().getDrawable(R.drawable.mark_type2)).getBitmap();
            newicon =  Bitmap.createScaledBitmap(bitmap,32,32,false);
            bitmap.recycle();
            imgeList.add(newicon);
            bitmap  =   ((BitmapDrawable) appContext.getResources().getDrawable(R.drawable.mark_type3)).getBitmap();
            newicon =  Bitmap.createScaledBitmap(bitmap,32,32,false);
            bitmap.recycle();
            imgeList.add(newicon);
            bitmap  =   ((BitmapDrawable) appContext.getResources().getDrawable(R.drawable.mark_type4)).getBitmap();
            newicon =  Bitmap.createScaledBitmap(bitmap,32,32,false);
            bitmap.recycle();
            imgeList.add(newicon);
        }
    }
}
